package sqlanalysisgui.util;

import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;

import sqlanalysisgui.editor.SqlSourceEditor;
import sqlanalysisgui.view.model.LineInfo;

public class GUIUtils {
	public static void goToLine(IEditorPart editorPart, int lineNumber) {
		if (!(editorPart instanceof ITextEditor) || lineNumber <= 0) {
			return;
		}
		ITextEditor editor = (ITextEditor) editorPart;
		IDocument document = editor.getDocumentProvider().getDocument(
				editor.getEditorInput());
		if (document != null) {
			IRegion lineInfo = null;
			try {
				// line count internaly starts with 0, and not with 1 like in
				// GUI
				lineInfo = document.getLineInformation(lineNumber - 1);
			} catch (BadLocationException e) {
				// ignored because line number may not really exist in document,
				// we guess this...
			}
			if (lineInfo != null) {
				editor.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength());
			}
		}
	}
	
	public static void textEditorSetHighlight(IEditorPart editorPart, int lineNumber) {
		if (!(editorPart instanceof ITextEditor) || lineNumber <= 0) {
			return;
		}
		ITextEditor editor = (ITextEditor) editorPart;
		IDocument document = editor.getDocumentProvider().getDocument(
				editor.getEditorInput());
		if (document != null) {
			IRegion lineInfo = null;
			try {
				// line count internaly starts with 0, and not with 1 like in
				// GUI
				lineInfo = document.getLineInformation(lineNumber - 1);
			} catch (BadLocationException e) {
				// ignored because line number may not really exist in document,
				// we guess this...
			}
			if (lineInfo != null) {
				editor.setHighlightRange(lineInfo.getOffset(), lineInfo.getLength(), false); 
			}
		}
	}
	public static void textEditorSetHighlight(int lineNumber) {
		SqlSourceEditor activeEditor = (SqlSourceEditor)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		
		if (!(activeEditor instanceof ITextEditor) || lineNumber <= 0) {
			return;
		}
		ITextEditor editor = (ITextEditor) activeEditor;
		IDocument document = editor.getDocumentProvider().getDocument(
				editor.getEditorInput());
		if (document != null) {
			IRegion lineInfo = null;
			try {
				// line count internaly starts with 0, and not with 1 like in
				// GUI
				lineInfo = document.getLineInformation(lineNumber - 1);
			} catch (BadLocationException e) {
				// ignored because line number may not really exist in document,
				// we guess this...
			}
			if (lineInfo != null) {
				editor.setHighlightRange(lineInfo.getOffset(), lineInfo.getLength(), false); 
			}
		}
	}
	
	public static void textEditorSetHighlights(List<LineInfo> lineInfos){
		for (LineInfo lineInfo : lineInfos) {
			textEditorSetHighlight(lineInfo.getLineInteger());
		}
	}
	
	public static void textEditorSetLineBackgroundColor(List<LineInfo> lineInfos){
		SqlSourceEditor activeEditor = (SqlSourceEditor)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		
		StyledText styledText = activeEditor.getStyledText();
		for (LineInfo lineInfo : lineInfos) {
			styledText.setLineBackground(10, 5, new Color(activeEditor.parent.getShell().getDisplay(), 255, 127, 0));
			//setLineBackground(lineInfo.getLineInteger(), 1, activeEditor.parent.getShell().getDisplay().getSystemColor(SWT.COLOR_YELLOW));
		}
		
	}
	
}
